package dps;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.UUID;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

public class DPS_GUI extends JFrame {
	
	private static final long serialVersionUID = 1L;
	//Top Panel
	JTextField txtPollParam;
	JPanel pnlTop;
	JLabel lblRunPoll;
	JButton btnPoll;
	
	//Bottom Panel
	JLabel lblTotalYesVotes;
	JLabel lblTotalNoVotes;
	JLabel lblTotalNoResponseVotes;
	JLabel lblFinalPollResult;
	
	//DOT
	JPanel pnlDOT;
	DefaultTableModel dotModel;
	JTable dotTable;
	JScrollPane dotScrollPane;
	
	//Poll History
	JPanel pnlPollH;
	DefaultTableModel pollHModel;
	JTable pollHTable;
	JScrollPane pollHScrollPane;
	
	public DPS_GUI() {

		// Top Panel
		pnlTop = new JPanel(new GridLayout(1, 1, 1, 1));
		lblRunPoll = new JLabel("Run Poll on:");
		txtPollParam = (new JTextField(20));
		btnPoll = new JButton(" Start Poll");
		// btnPoll.addActionListener(new PollListener());
		btnPoll.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					DpsMain.CreateAndSendPollMsg(txtPollParam.getText());
				} catch (UnknownHostException e1) {
					e1.printStackTrace();
				} catch (SocketException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});

		pnlTop.add(lblRunPoll);
		pnlTop.add(txtPollParam);
		pnlTop.add(btnPoll);
		pnlTop.setBorder(new TitledBorder("Poll"));

		// Buttom Panel
		JPanel pnlBottom = new JPanel(new GridLayout(4, 2, 5, 5));
		Font font = new Font("Result", Font.BOLD, 18);
		
		//Vote: Yes
		JLabel lblYesVote = new JLabel("Total Yes Votes: ");
		lblTotalYesVotes = new JLabel("");
		lblTotalYesVotes.setForeground(Color.GREEN);

		//Vote: No
		JLabel lblNoVote = new JLabel("Total No Votes: ");
		lblTotalNoVotes = new JLabel("");
		lblTotalNoVotes.setForeground(Color.RED);

		//Vote: No Response
		JLabel lblNoResponseVote = new JLabel("Total No Response Votes: ");
		lblTotalNoResponseVotes = new JLabel("");
		lblTotalNoResponseVotes.setForeground(Color.ORANGE);

		//Poll Result
		JLabel lblPollResult = new JLabel("Poll Result: ");
		lblFinalPollResult = new JLabel("");
		lblFinalPollResult.setFont(font);

		//Adding controls to panel
		pnlBottom.add(lblYesVote);
		pnlBottom.add(lblTotalYesVotes);
		pnlBottom.add(lblNoVote);
		pnlBottom.add(lblTotalNoVotes);
		pnlBottom.add(lblNoResponseVote);
		pnlBottom.add(lblTotalNoResponseVotes);
		pnlBottom.add(lblPollResult);
		pnlBottom.add(lblFinalPollResult);

		pnlTop.setBorder(new TitledBorder("Poll"));
		pnlBottom.setBorder(new TitledBorder("Poll Result"));
		
		//Degree of Trust Grid
		pnlDOT = new JPanel(new GridLayout(1, 1, 1, 1));
		String dotData [][] = {
		};

		String dotCol [] = {"Voter Id","Degree of Trust"};
		
		dotModel = new DefaultTableModel(dotData,dotCol);
		dotTable = new JTable(dotModel);
		dotTable.setShowGrid(true);
		dotTable.setShowVerticalLines(true);
		
		dotScrollPane = new JScrollPane(dotTable);
		pnlDOT.add(dotScrollPane);
		
		//Poll History Grid
		pnlPollH = new JPanel(new GridLayout(1, 1, 1, 1));
		String pollHData [][] = {
		};

		String pollHCol [] = {"Poll Param","Yes", "No", "No Response", "Poll Result"};
		
		pollHModel = new DefaultTableModel(pollHData,pollHCol);
		pollHTable = new JTable(pollHModel);
		pollHTable.setShowGrid(true);
		pollHTable.setShowVerticalLines(true);
		
		pollHScrollPane = new JScrollPane(pollHTable);
		pnlPollH.add(pollHScrollPane);
		
		//Frame setting
		JPanel lo = new JPanel(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		
		c.fill = GridBagConstraints.HORIZONTAL;
		c.gridy = 0;
		lo.add(pnlTop, c);
		c.gridx = 1;
		lo.add(pnlBottom, c);
		c.gridy = 2;
		c.gridx = 0;
		c.ipadx = 400;
		c.ipady = 200;
		lo.add(pnlDOT, c);
		c.gridy = 2;
		c.gridx = 1;
		lo.add(pnlPollH, c);
		add(lo);
		pack();
	}
	
	
	//Populates the buttom panel with the specific poll result
	public void PollResult(Poll poll)
	{
		lblTotalYesVotes.setText(String.valueOf(poll.pollVoteYes));
		lblTotalNoVotes.setText(String.valueOf(poll.pollVoteNo));
		lblTotalNoResponseVotes.setText(String.valueOf(poll.pollVoteNoResponse));
		
		//Poll result with proper color
		switch( poll.getMajority().toString() )
		 {
			 case "YES" : 
				 lblFinalPollResult.setText("Yes");
				 lblFinalPollResult.setForeground(Color.GREEN);
				 break;
			 case "NO" : 
				 lblFinalPollResult.setText("No"); 
				 lblFinalPollResult.setForeground(Color.RED);
				 break;
			 case "NoResponse" : 
				 lblFinalPollResult.setText("NoResponse"); 
				 lblFinalPollResult.setForeground(Color.ORANGE);
				 break;
			 default : 
				 lblFinalPollResult.setText("NoResponse");
				 lblFinalPollResult.setForeground(Color.ORANGE);
		 }
	}
	
	
	public void DOTGrid(Map<UUID,Trust> hmTrust)
	{
		String tData[][] = new String[hmTrust.size()][2];
		int i=0,j=0;
		
		for(Map.Entry<UUID, Trust> t :hmTrust.entrySet()){
        	tData[i][j++]=t.getValue().voterId.toString();
        	tData[i][j++]=String.valueOf(t.getValue().degreeOfTrust);
        	i++;
        	j=0;
		}
        
        String tCol [] = {"Voter Id","Degree of Trust"};
        dotModel.setDataVector( tData, tCol );
	}
	
	public void PollHistoryGrid(Map<UUID,Poll> hmPoll)
	{
		String pData[][] = new String[hmPoll.size()][5];
		int i=0,j=0;
		
		for(Map.Entry<UUID, Poll> p :hmPoll.entrySet()){
        	pData[i][j++]=p.getValue().strPollParam;
        	pData[i][j++]=String.valueOf(p.getValue().pollVoteYes);
        	pData[i][j++]=String.valueOf(p.getValue().pollVoteNo);
        	pData[i][j++]=String.valueOf(p.getValue().pollVoteNoResponse);
        	pData[i][j++]=p.getValue().getMajority().toString();
        	i++;
        	j=0;
		}
        
        String pCol [] = {"Poll Param","Yes", "No", "No Response", "Poll Result"};
        pollHModel.setDataVector(pData, pCol);
	}
}
